<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="50%" >
    <el-form
      ref="formRef"
      :model="keyword"
      label-width="50px"
      v-loading="formLoading"
      :inline="true"
    >
      <el-form-item label="姓名" prop="keyword">
        <el-input v-model="keyword" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item>
        <el-button @click="getList" type="primary"> 搜索</el-button>
        <el-button @click="resetQuery"> 重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      border
      v-loading="formLoading"
      ref="multipleTableRef"
      :data="list"
      @selection-change="handleSelectionChange"
      max-height="400px"
      row-key="id"
    >
      <el-table-column type="selection"/>
      <el-table-column label="工号" header-align="center" align="left" prop="jobNumber" />
      <el-table-column label="姓名" header-align="center" align="left" prop="staffName" />
      <el-table-column label="所属班组" header-align="center" align="left" prop="teamName" />
      <el-table-column label="所属部门" header-align="center" align="left" prop="configOrgDeptArchivesName" />
    </el-table>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import * as ComponentApi from '@/api/componentApi/index'

const keyword = ref('')
const formLoading = ref(false)
const formRef = ref()
const dialogVisible = ref(false)
const list = ref([])
const multipleTableRef = ref()
const multipleSelection = ref([])
const editIndex = ref(0)
const editParentIndex = ref()
const dialogTitle =ref ("")
/** 重置按钮操作 */
const resetQuery = () => {
  keyword.value = ''
  getList()
}

// 打开弹窗
const open = async (arr: [], index: number, hasChild?: boolean, parentIndex?: number) => {
  dialogVisible.value = true
  editIndex.value = index
  // 指派子类
  if (hasChild) {
    editParentIndex.value = parentIndex
  }
  nextTick(() => {
    multipleTableRef.value!.clearSelection()
  })
  keyword.value = ''
  multipleSelection.value = []
  await getList()
  nextTick(() => {
    if(arr){
      arr.map(val => {
      list.value.map(item => {
        if(val === item['jobNumber']) {
          multipleTableRef.value!.toggleRowSelection(item, true)
        }
      })
    })
    }
  })
}
defineExpose({ open, multipleSelection }) // 提供 open 方法，用于打开弹窗

// 获取数据
const getList = async () => {
  formLoading.value = true
  try {
    list.value = await ComponentApi.getStaffList(keyword.value)
  } finally {
    formLoading.value = false
  }
}

// 多选
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

const emit = defineEmits(['setStaff'])
const submitForm = () => {
  dialogVisible.value = false
  if (editParentIndex.value !== undefined) {
    emit('setStaff', editIndex.value, editParentIndex.value)
  } else {
    emit('setStaff', editIndex.value)
  }
}
</script>